

package cn.zyx.controller;

import cn.zyx.dao.VipDao;
import cn.zyx.entity.Vip;
import cn.zyx.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Vip)表控制层
 *
 * @author makejava
 * @since 2022-01-09 16:48:07
 */
@RestController
@RequestMapping("vip")
public class VipController {
    /**
     * 服务对象
     */
    @Resource
    private VipService vipService;
    @Resource
    private VipDao vipDao;

    @Autowired
    private ReturnPayApi returnPayApi;



    /**
     * 根据wx手机号 查询
     */
    @RequestMapping("selphone")
    public List<Vip> selwxPhone(String wxPhone){
        return vipService.queryPhone(wxPhone);
    }
    /**
     * 根据订单编号查询
     */
    @RequestMapping("selnumber")
    public  Vip selorderno(String orderno){
        return vipService.queryOrderno(orderno);
    }

    /**
     * 视频会员订单添加
     * @param vip
     * @return
     */
    @RequestMapping("add")
    public Vip add(@RequestBody Vip vip) {
        VipController vipController = new VipController();
        String time = vipController.time();
        System.out.println(vip);
//        添加下单时间
        vip.setCreateTime(time);
//        添加充值状态
        vipService.insert(vip);
        return vip;
    }

    /**
     * 订单号和外部订单号
     *
     * @param
     * @return
     */
    @RequestMapping("sel")
    public Map<String, Object> query(@RequestParam(defaultValue = "1") Integer page, Integer limit,String orderno,String outorderno){

        List<Vip> vip = this.vipService.queryAllByLimit((page - 1) * limit, limit,orderno,outorderno);
        for (int i = 0; i < vip.size(); i++) {
            String orderno1 = vip.get(i).getOrderno();
//            String outorderno = vip.get(i).getOutorderno();
        }
        int count = vipDao.count(orderno,outorderno);
        Map<String, Object> map = new HashMap<>();
        map.put("success", true);
        map.put("msg","查询成功");
        map.put("data", vip);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }


    /**
     * 将未充值用户进行充值
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("cupd")
    public Map<String, Object> cupd(String id) {
        Vip vip = new Vip();
        vip.setId(id);
        vip.setStatus("3");
        VipController vipController = new VipController();
        String time = vipController.time();
        vip.setCreateTime(time);
        vipService.update(vip);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }


    /**
     * 将充值完成或者退款成功记录进行删除
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("dupd")
    public Map<String, Object> dupd(String id) {
        Vip vip = new Vip();
        vip.setId(id);
        vip.setIsdel(1);
        OilController oilController = new OilController();
        String time = oilController.time();
        vip.setCreateTime(time);
        vipService.update(vip);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @RequestMapping("dupds")
    public Map<String, Object> dupds(String idsStr) {
        String[] split = idsStr.split(",");
        for (int i = 0; i < split.length; i++) {
            Vip vip = new Vip();
            String s = split[i];
            vip.setId(s);
            vip.setIsdel(1);
            vipService.update(vip);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }


    /**
     * 将充值失败用户进行退款
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("tupd")
    public Map<String, Object> tupd(String id, String orderNum, String money, String desc, HttpServletResponse response) {
        Vip vip1 = vipService.queryById(id);
//        int a = (int) (Float.valueOf(vip1.getPaymoney())*100);
//        returnPayApi.refund(vip1.getOutorderno(),"",String.valueOf(a), response);
//        Map<String, Object> refund = returnPayApi.refund(vip1.getOutorderno(), "", "1", response);
        System.out.println(id);
        Vip vip = new Vip();
        vip.setId(id);
        vip.setType("2");
        VipController VipController = new VipController();
        String time = VipController.time();
        vip.setCreateTime(time);
        vip.setOrderno(orderNum);
        vip.setMoney(money);
        vip.setDesc(desc);
        vipService.update(vip);
        Map<String, Object> map = new HashMap<>();
        map.put("isOk", true);
        map.put("code", 0);
        return map;
    }






    public String time() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        return format;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
        @GetMapping("selectOne")
    public Vip selectOne(String id) {
        return this.vipService.queryById(id);
    }

}